angular.module('wf-utils')
.service('menuManage', ($rootScope,$http,$localStorage,$state)->
    states = {
        #欢迎页/控制台
        'welcome':{
            name:'欢迎页面',
            icon:'fa-home',
            url:'welcome/welcome.html',
            controller:'WFWelcomeCtrl'
        },
        #系统
        'role-manage': {
            name: '角色配置',
            url: "system/role/role-manage.html",
            controller: "WFRoleManageCtrl"
        },
        'role-setting':{
            name: '角色菜单配置',
            url: "system/role/role-setting.html",
            controller: "WFRoleSettingCtrl"
        },
        'menu-manage': {
            name: '菜单配置',
            url: "system/menu/menu-manage.html",
            controller: "WFMenuManageCtrl"
        },
        'account-manage': {
            name: '账号配置',
            url: "system/account/account-manage.html",
            controller: "WFAccountManageCtrl"
        },
        'account-manage-edit': {
            name: '账号明细编辑',
            url: "system/account/account-manage-edit.html",
            controller: "WFAccountManageEditCtrl"
        },
        'authority-manage': {
            name: '权限配置',
            url: "system/authority/authority-manage.html",
            controller: "WFAuthorityManageCtrl"
        },
        'authority-manage-edit': {
            name: '权限编辑页面',
            url: "system/authority/authority-manage-edit.html",
            controller: "WFAuthorityManageEditCtrl"
        },
        'resource-manage': {
            name: '资源配置',
            url: "system/resource/resource-manage.html",
            controller: "WFResourceManageCtrl"
        },
        #参数配置
        'basic-params':{
            name: '参数配置',
            url: "system/parameters/basic-params.html",
            controller: "WFBasicParamsCtrl"
        }
        'params-setting':{
            name: '参数字典配置',
            url: "system/parameters/params-setting.html",
            controller: "WFParamsSettingCtrl"
        }
        'wi-customer':{
            name: '用户管理',
            url: "wi/customer/customer-manage.html",
            controller: "WICustomerManageCtrl"
        }
        'wi-customer-edit':{
            name: '用户编辑',
            url: "wi/customer/customer-manage-edit.html",
            controller: "WICustomerManageEditCtrl"
        }
        'wi-raspberry':{
            name: '树莓派管理',
            url: "wi/raspberry/raspberry-manage.html",
            controller: "WIRaspberryManageCtrl"
        }
        'wi-raspberry-edit':{
            name: '树莓派编辑',
            url: "wi/raspberry/raspberry-manage-edit.html",
            controller: "WIRaspberryManageEditCtrl"
        }



    }
    # type{0-iframe,1-快捷操作}
    menuList = [
        {
            id: 1
            name: '菜单名称'
            url:'http://localhost:8100/#/'
            icon: 'fa-home'
            isMenu:false
            isInnerRoute:false
            hasUrlModules:true
            urlModules:[{
                id:2,
                order:0,
                type:0,
                title:'模块名称0',
                url:'http://localhost:8100',
                hideTitle:false,
                needMore:true,
                moreUrl:'http://localhost:8100',
                titleStyle:{
                    background:'#000',
                    color:'#000',
                    borderRadius: '50px'
                },
                containerStyle:{
                    border:"5px solid #444"
                    background:'#000',
                    color:'#000',
                    borderRadius: '50px'
                },
                width:'75%',
                height:'300px'
            },
                {
                    id:12,
                    order:0,
                    type:0,
                    title:'模块名称0',
                    url:'http://localhost:8100',
                    hideTitle:false,
                    titleStyle:{
                        background:'#000',
                        color:'#000',
                        borderRadius: '50px'
                    },
                    width:'25%',
                    height:'300px'
                }]
        }
        {
            id: 3
            name: '菜单名称'
            url:'http://localhost:8100/#/'
            icon: 'fa-home'
            isMenu:true
            isInnerRoute:true
            hasUrlModules:true
            urlModules:[{
                id:4,
                order:0,
                type:0,
                title:'模块名称0',
                url:'http://localhost:8100',
                hideTitle:false,
                titleStyle:{
                    background:'#f00',
                    color:'#fff',
                    borderRadius: '50px'
                },
                containerStyle:{
                    border:"5px solid #444"
                    background:'#000',
                    color:'#000',
                    borderRadius: '50px'
                },
                width:'80%',
                height:'300px'
            }]
            subMenus:[
                {
                    id: 5
                    name: '菜单名称'
                    url:'http://localhost:8100/#/'
                    icon: 'fa-home'
                    isMenu:false
                    isInnerRoute:false
                    hasUrlModules:true
                    urlModules:[{
                        id:2,
                        order:0,
                        type:0,
                        title:'模块名称0',
                        url:'http://localhost:8100',
                        hideTitle:false,
                        titleStyle:{
                            background:'#0f0',
                            color:'#fff',
                            borderRadius: '50px'
                        },
                        width:'80%',
                        height:'300px'
                    }]
                }
            ]
        }
    ]
    get: ()->
        if $localStorage.menuList == undefined
            $localStorage.menuList = menuList
        $localStorage.menuList

    getRoleMenu: (roleID)->
        menuList
    goState:(state,reload)->
        if states[state]
            item={
                href:state
                name:states[state].name
                icon:states[state].icon
            }
            this.goPage(item,reload)
    goPage: (item,reload)->
        href = item.href
        if $rootScope.navTabs.active == href
            return
        tmp = _.find $rootScope.navTabs.arrs, (tab)-> tab.href == href
        if !tmp
            $rootScope.navTabs.arrs.push {href: href, name: item.name, icon: item.icon}
        else
            if reload
                $rootScope.navTabs.arrs=_.filter($rootScope.navTabs.arrs,(tab)->tab.href!=href)
                $rootScope.navTabs.history = _.without $rootScope.navTabs.history, href
                $rootScope.navTabs.arrs.push {href: href, name: item.name, icon: item.icon}
        $rootScope.navTabs.active = href
        tmpHistory = _.find $rootScope.navTabs.history, (history)-> history == href
        if tmpHistory
            $rootScope.navTabs.history = _.without $rootScope.navTabs.history, tmpHistory
        $rootScope.navTabs.history.push href
#        console.log('navTabs:', $rootScope.navTabs)
    genViews: ()->
        views = {"":{templateUrl:'templates/main/newmain.html',controller:'WFMainCtrl'}}
        _.each states, (item,param)->
#            console.log('state:',param)
            views[param + '@main'] = {
                templateUrl: 'templates/' + item.url
                controller: item.controller
            }
#        console.log(views)
        views
    closeTab:(event,href)->
        if event
            event.preventDefault()
        $rootScope.navTabs.history=_.filter $rootScope.navTabs.history,(history)->history!=href
        $rootScope.navTabs.arrs=_.filter $rootScope.navTabs.arrs,(tab)->tab.href!=href
        if $rootScope.navTabs.active==href and $rootScope.navTabs.history.length>=1
            $rootScope.navTabs.active=$rootScope.navTabs.history[$rootScope.navTabs.history.length-1]
    route:(item,preventStack)->
        console.log "route",item
        window.localStorage.currentHash = 0
        $localStorage.currentHash = item?.id
        if preventStack
            $localStorage.currentMenu = item
            $rootScope.$broadcast('refreshLayoutContainer')
        else
            if (""+item.url).startWith("http://")
                console.log item?.url
                window.open item?.url
            else
                if eval(item?.isInnerRoute)
                    $localStorage.currentMenu = item
                    $localStorage.currentHash = item?.id
                    console.log("$broadcastrefreshLayoutContainer")
                    $rootScope.$broadcast('refreshLayoutContainer')
                else
                    $localStorage.newMenu = item
                    $localStorage.newHash = item?.id
                    $rootScope.$broadcast('newLayoutContainer')




)